package com.ywjh.inter.service;

import java.sql.Connection;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.ywjh.inter.conn.DBConnection;
import com.ywjh.inter.dao.InsulinPenInjectDAO;
import com.ywjh.inter.entity.InsulinPenGlucoseEntity;
import com.ywjh.inter.entity.InsulinPenInjectEntity;
import com.ywjh.inter.entity.InsulinPenInjectSetEntity;
import com.ywjh.inter.utils.DateUtils;

public class InsulinPenInjectService {
	
	private static InsulinPenInjectService instance;
	private InsulinPenInjectDAO insulinPenInjectDAO;
	
	public static InsulinPenInjectService getInstance() {
		if(instance == null) {
			instance = new InsulinPenInjectService();
			instance.insulinPenInjectDAO = new InsulinPenInjectDAO();
		}
		return instance;
	}

	public boolean add(InsulinPenInjectEntity entity) {
		boolean b = false;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			b = insulinPenInjectDAO.insert(conn, entity);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return b;
	}
	
	public List<InsulinPenInjectEntity> getBySnAndTime(String sn, String date) {
		Date d1 = DateUtils.parseDate(date);
		Calendar c1 = Calendar.getInstance();
		c1.setTime(d1);
		Calendar c2 = Calendar.getInstance();
		c2.set(c1.get(Calendar.YEAR), c1.get(Calendar.MONTH), c1.get(Calendar.DATE), 23, 59, 59);
		List<InsulinPenInjectEntity> r = null;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			r = insulinPenInjectDAO.getBySnAndTime(conn, sn, d1.getTime(), c2.getTime().getTime());
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return r;
	}
	
	public InsulinPenInjectEntity getNewBySn(String sn) {
		InsulinPenInjectEntity b = null;
		Connection conn = null;
		try {
			conn = DBConnection.getDBConnection();
			b = insulinPenInjectDAO.getNewBySn(conn, sn);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			DBConnection.close(conn);
		}
		return b;
	}
}
